<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="告警名称" prop="alarmName">
      <el-input v-model="dataForm.alarmName" placeholder="告警名称"></el-input>
    </el-form-item>
    <el-form-item label="IP地址" prop="ipAddress">
      <el-input v-model="dataForm.ipAddress" placeholder="IP地址"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="端口号" prop="portNumber">
      <el-input v-model="dataForm.portNumber" placeholder="端口号"></el-input>
    </el-form-item>
    <el-form-item label="编码方式" prop="encodingMethod">
      <el-input v-model="dataForm.encodingMethod" placeholder="编码方式"></el-input>
    </el-form-item>
    <el-form-item label="通信协议" prop="protocol">
      <el-input v-model="dataForm.protocol" placeholder="通信协议"></el-input>
    </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="dataForm.deviceType" placeholder="请选择">
          <el-option
            v-for="item in deviceTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="报警类型" prop="alarmType">
      <el-select v-model="dataForm.alarmType" placeholder="请选择">
        <el-option
          v-for="item in doorStatuses"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>

    </el-form-item>
    <el-form-item label="报警数据" prop="alarmData">
      <el-input v-model="dataForm.alarmData" placeholder="报警数据"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/hik/alarm'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        doorStatuses: [{
          value: '0',
          label: '设备在线'
        }, {
          value: '1',
          label: '设备离线'
        }],
        deviceTypes: [{
          value: '1',
          label: '门禁'
        }, {
          value: '2',
          label: '闸机'
        }, {
          value: '3',
          label: '摄像头'
        }, {
          value: '4',
          label: '人脸设备'
        }, {
          value: '5',
          label: '道闸'
        }, {
          value: '6',
          label: '车道识别'
        }],
        dataForm: {
          alarmId: 0,
          alarmName: '',

          ipAddress: '',

          username: '',

          password: '',

          portNumber: '',
          deviceType:'',

          encodingMethod: '',

          protocol: '',

          alarmType: '',

          alarmData: '',

          createTime: '',

          updateTime: '',

          delFlag: '',

        },
        dataRule: {
          alarmName: [
            { required: true, message: '告警名称不能为空', trigger: 'blur' }
          ],

          ipAddress: [
            { required: true, message: 'IP地址不能为空', trigger: 'blur' }
          ],

          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],

          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],

          portNumber: [
            { required: true, message: '端口号不能为空', trigger: 'blur' }
          ],

          encodingMethod: [
            { required: true, message: '编码方式不能为空', trigger: 'blur' }
          ],

          protocol: [
            { required: true, message: '通信协议不能为空', trigger: 'blur' }
          ],

          alarmType: [
            { required: true, message: '报警类型不能为空', trigger: 'blur' }
          ],
          deviceType: [
            { required: true, message: '设备类型不能为空', trigger: 'blur' }
          ],
          alarmData: [
            { required: true, message: '报警数据不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.alarmId = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.alarmId) {
            getObj(this.dataForm.alarmId).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.alarmId) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      }
    }
  }
</script>
